//
//  UITool.m
//  WeCommunity
//
//  Created by mac on 2017/6/14.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "UITool.h"

@implementation UITool

#pragma mark - 获取当前屏幕显示的viewcontroller
+ (UIViewController *)getCurrentVC
{
    UIViewController *result = nil;
    
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows)
        {
            if (tmpWin.windowLevel == UIWindowLevelNormal)
            {
                window = tmpWin;
                break;
            }
        }
    }
    
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    
    if ([nextResponder isKindOfClass:[UIViewController class]]){
        result = nextResponder;
    }else{
        result = window.rootViewController;
    }
    
    UITabBarController * tabController = (UITabBarController*)result;
    
    UINavigationController * navc = tabController.selectedViewController ;
    result = navc.viewControllers.lastObject;
    while (result.presentedViewController) {
        result = result.presentedViewController;
    }
    
    return result;
}

#pragma mark - alert提醒
+(void)showHudWithText:(NSString *)text
              withView:(UIView *)view
{
    UIView * currentView;
    if (view) {
        currentView = view;
    }else{
        currentView = [UITool getCurrentVC].view;
    }

    MBProgressHUD * hud  = [[MBProgressHUD alloc] initWithView:currentView];
    [currentView addSubview:hud];
    hud.labelText = text;
    hud.labelFont = PFR_FONT(12);
    hud.mode = MBProgressHUDModeText;
    [hud showAnimated:YES whileExecutingBlock:^{
        sleep(1);
    } completionBlock:^{
        [hud removeFromSuperview];
    }];
}


-(void)showLoadingHudWithHText:(NSString *)text withView:(UIView *)view
{
    if (text.length == 0 || !text) {
        text = KNSLocal(@"loadding");
    }
    UIView * currentView;
    if (view) {
        currentView = view;
    }else{
        currentView = [UITool getCurrentVC].view;
    }
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:currentView animated:YES];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.labelText = text;
    hud.labelFont = PFR_FONT(12);
}

-(void)hiddenLoadingHudwithView:(UIView *)view
{
    UIView * currentView;
    if (view) {
        currentView = view;
    }else{
        currentView = [UITool getCurrentVC].view;
    }
    [MBProgressHUD hideHUDForView:currentView animated:YES];
}

#pragma mark - 设置状态栏
+(void)setStatusBarIsWhite:(BOOL)isWhite
{
    if (isWhite) {
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    }else{
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    }
}

#pragma mark - textField 长度限制
+(void)limitTextFieldLength:(NSInteger)maxLimit textField:(UITextField *)textField
{
    UITextRange *selectedRange = [textField markedTextRange];
    //获取高亮部分
    UITextPosition *pos = [textField positionFromPosition:selectedRange.start offset:0];
    //如果在变化中是高亮部分在变，就不要计算字符了
    if (selectedRange && pos) {
        return;
    }
    NSString  *nsTextContent = textField.text;
    NSInteger existTextNum = nsTextContent.length;
    if (existTextNum > maxLimit)
    {
        //截取到最大位置的字符(由于超出截部分在should时被处理了所在这里这了提高效率不再判断)
        NSString *s = [nsTextContent substringToIndex:maxLimit];
        [textField setText:s];
    }
}

+(void)textCopyWithString:(NSString *)string
{
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    pasteboard.string = string;
}

#pragma mark - 让所有控制器出栈
+(void)removeAllController
{
    UIViewController * preVC = [UIControllerCenter shared].rootTabBarController.presentedViewController;
    [preVC dismissViewControllerAnimated:NO completion:nil];
    
    RootNaviViewController * navc = [UIControllerCenter shared].rootTabBarController.selectedViewController;
    if (navc.viewControllers.count > 1) {
        UIViewController * subVC = navc.viewControllers.lastObject;
        [subVC.navigationController popToRootViewControllerAnimated:NO];
    }
}

#pragma mark - 设置SD框架解压缩图片
+(void)setSDWebDecompression:(BOOL)decompression
{
    SDImageCache *canche = [SDImageCache sharedImageCache];
    canche.shouldDecompressImages = decompression;
    
    SDWebImageDownloader *downloder = [SDWebImageDownloader sharedDownloader];
    downloder.shouldDecompressImages = decompression;
}


#pragma mark - 一般不传title title有默认内容
+(void)systemAlertWithTitle:(NSString *)title
                 andMessage:(NSString *)message
              actionTextOne:(NSString *)actionTextOne
              actionTextTwo:(NSString *)actionTextTwo
             actionOneBlock:(void(^)())actionOneBlock
             actionTwoBlock:(void(^)())actionTwoBlock
{
    if (title.length == 0 || !title) {
        title = KNSLocal(@"alertTitle");
    }
    dispatch_async(dispatch_get_main_queue(), ^{
        UIAlertController *alertController=[UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
        
        if (actionTextOne.length > 0 &&  actionTextOne) {
            UIAlertAction * actionOne =[UIAlertAction actionWithTitle:actionTextOne style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    actionOneBlock();
                });
            }];
            [alertController addAction:actionOne];
        }
        if (actionTextTwo.length > 0 && actionTextTwo) {
            UIAlertAction * actionTwo = [UIAlertAction actionWithTitle:actionTextTwo style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    actionTwoBlock();
                });
            }];
            [alertController addAction:actionTwo];
        }
        [[UITool getCurrentVC] presentViewController:alertController animated:YES completion:nil];
    });
}

@end
